## Calculation of Local Government Efficiency Scores
## Who gets what -- and how efficiently? Incorporating distributional concerns into the measurement of local government efficiency
## Last modified: July 26 2019

## Install and load necessary packages

# install.packages("Benchmarking")
# install.packages("glpkAPI")
# install.packages("Rglpk")
# install.packages("readstata13")

library(Benchmarking)
library(glpkAPI)
library(Rglpk)
library(boot)
library(stats)
library(MASS)
library(foreign)
library(readstata13)

rm(list = ls())
data_file <- read.dta13("vfm_dataset_clean_small_2.dta")
attach(data_file)

## Calculate efficiency scores using Benchmarking
x_var <- cbind(funds_used_mil) # Input = Money spent on water in district 
y_var <- cbind(wpts_built_wt) # Output for Spending Efficiency measure = Number of water points built (weighted by tech type)
y_var_access <- cbind(acc_imp_06_13_num*100) # Output for Access Efficiency measure = Percentage Point Improvement in Access

# Calculation of Spending Efficiency
e <-dea.boot(x_var, y_var, NREP = 200, EFF = NULL, RTS = "vrs", ORIENTATION="in",
             alpha = 0.05, XREF = NULL, YREF = NULL, EREF = NULL,
             DIRECT = NULL, TRANSPOSE = FALSE, SHEPHARD.INPUT = TRUE)

dea_spend <- cbind(eff(e))

# Calculation of Access Efficiency
e_access <-dea.boot(x_var, y_var_access, NREP = 200, EFF = NULL, RTS = "vrs", ORIENTATION
                    ="in",
                    alpha = 0.05, XREF = NULL, YREF = NULL, EREF = NULL,
                    DIRECT = NULL, TRANSPOSE = FALSE, SHEPHARD.INPUT = TRUE)

dea_access <- cbind(eff(e_access))